package hospital.controller.api;

import hospital.common.helper.HttpRequestHelper;
import hospital.exception.HospitalException;
import hospital.model.hosp.Department;
import hospital.model.hosp.Hospital;
import hospital.model.hosp.Schedule;
import hospital.result.Result;
import hospital.result.ResultCodeEnum;
import hospital.service.DepartmentService;
import hospital.service.HospitalService;
import hospital.service.HospitalSetService;
import hospital.service.ScheduleService;
import hospital.utils.manageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @description:
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService service;
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * @Author yang
     * @Description //TODO 删除排班接口
     * @Param
     * @return
     **/
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        //获取request的请求参数
        Map<String, String[]> requestParameter = request.getParameterMap();
        //转化为Map<String, Object >
        Map<String, Object>  parameterMap= HttpRequestHelper.switchMap(requestParameter);

        //判断医院的签名是否一致
        if(!manageUtils.checkSignKey(parameterMap,hospitalSetService)){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        //根据hoscode hosScheduleId 删除科室信息
        scheduleService.removeSchedule(parameterMap);
        return Result.ok();
    }
    /**
     * @Author yang
     * @Description //TODO 查询排班接口
     * @Param
     * @return
     **/
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取request的请求参数
        Map<String, String[]> requestParameter = request.getParameterMap();
        //转化为Map<String, Object >
        Map<String, Object>  parameterMap= HttpRequestHelper.switchMap(requestParameter);

        //判断医院的签名是否一致
        if(!manageUtils.checkSignKey(parameterMap,hospitalSetService)){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //得到排班的信息
        Page<Schedule> page =scheduleService.findSchedule(parameterMap);
        return Result.ok(page);
    }

    /**
     * @Author yang
     * @Description //TODO 上传排班接口
     * @Param
     * @return 
     **/
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取request的请求参数
        Map<String, String[]> requestParameter = request.getParameterMap();
        //转化为Map<String, Object >
        Map<String, Object>  parameterMap= HttpRequestHelper.switchMap(requestParameter);

        //判断医院的签名是否一致
        if(!manageUtils.checkSignKey(parameterMap,hospitalSetService)){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //保存或者修改排班方法
        scheduleService.saveOrUpdate(parameterMap);
        return Result.ok();
    }
    /**
     * @Author yang
     * @Description //TODO 删除科室接口
     * @Param
     * @return
     **/
    @PostMapping("department/remove")
    public Result deleteDepartment(HttpServletRequest request){
        //获取request的请求参数
        Map<String, String[]> requestParameter = request.getParameterMap();
        //转化为Map<String, Object >
        Map<String, Object>  parameterMap= HttpRequestHelper.switchMap(requestParameter);

        //判断医院的签名是否一致
        if(!manageUtils.checkSignKey(parameterMap,hospitalSetService)){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        //根据hoscode descode删除科室信息
        departmentService.deleteDepartment(parameterMap);
        return Result.ok();
    }
    /**
     * @Author yang
     * @Description //TODO 科室查询接口
     * @Param
     * @return
     **/
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取request的请求参数
        Map<String, String[]> requestParameter = request.getParameterMap();
        //转化为Map<String, Object >
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestParameter);

        //判断医院的签名是否一致
        if(!manageUtils.checkSignKey(parameterMap,hospitalSetService)){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //得到科室的信息
        Page<Department> page = departmentService.findDepartment(parameterMap);
        return Result.ok(page);
    }
    /**
     * @Author yang
     * @Description //TODO 上传科室接口
     * @Param
     * @return
     **/
    @PostMapping("saveDepartment")
    public Result  saveDepartment(HttpServletRequest request){
        //获取request的请求参数
        Map<String, String[]> requestParameter = request.getParameterMap();
        //转化为Map<String, Object >
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestParameter);

        //判断医院的签名是否一致
        if(!manageUtils.checkSignKey(parameterMap,hospitalSetService)){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //保存或修改科室的数据
        departmentService.saveOrUpdate(parameterMap);
        return Result.ok();
    }
    /**
     * @Author yang
     * @Description //TODO 医院查询接口
     * @Param
     * @return
     **/
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取request的请求参数
        Map<String, String[]> requestParameter = request.getParameterMap();
        //转化为Map<String, Object >
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestParameter);

        String hoscode= (String) parameterMap.get("hoscode");

        //判断医院的签名是否一致
        if(!manageUtils.checkSignKey(parameterMap,hospitalSetService)){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service,通过hoscode查询医院数据
        Hospital hospital=service.getHospital(hoscode);
        return Result.ok(hospital);
    }


    /**
     * @Author yang
     * @Description //TODO 上传医院的接口 或者更新接口
     * @Param
     * @return
     **/
    @PostMapping("saveHospital")
    public Result saveOrUpdate(HttpServletRequest request){
        //获取request的请求参数
        Map<String, String[]> requestParameter = request.getParameterMap();
        //转化为Map<String, Object >
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestParameter);

        //传输过程中 “+” 转化为了“ ”,需要转化回来
        String logoData= (String) parameterMap.get("logoData");
        logoData=logoData.replaceAll(" ","+");
        parameterMap.put("logoData",logoData);

        //判断医院的签名是否一致
        if(!manageUtils.checkSignKey(parameterMap,hospitalSetService)){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //保存或者修改医院的数据
        service.saveOrUpdate(parameterMap);
        return Result.ok();
    }
}
